<script setup lang="ts">
import { ref, onBeforeUnmount, shallowRef } from "vue";
import { formRules } from "./utils/rule";
import { FormProps } from "./utils/types";
import "@wangeditor/editor/dist/css/style.css";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { message } from "@/utils/message";
import { uploadImage, uploadVideo } from "@/api/sysupload";
import Motion from "@/views/login/utils/motion";

const props = withDefaults(defineProps<FormProps>(), {
  formInline: () => ({
    id: "",
    questionid: "",
    name: "",
    content: "",
    addtime: "",
    updatetime: ""
  })
});

const questionData0 = ref([
  { id: "A" },
  { id: "B" },
  { id: "C" },
  { id: "D" },
  { id: "E" },
  { id: "F" },
  { id: "G" },
  { id: "H" },
  { id: "I" },
  { id: "J" },
  { id: "K" },
  { id: "L" }
]);

const ruleFormRef = ref();
const newFormInline = ref(props.formInline);

function getRef() {
  return ruleFormRef.value;
}

defineExpose({ getRef });

const mode = "default";
// 编辑器实例，必须用 shallowRef
const editorRef = shallowRef();

const toolbarConfig: any = { excludeKeys: "fullScreen" };
const editorConfig = {
  placeholder: "请输入题目...",
  MENU_CONF: {
    uploadImage: {
      customUpload: async (file, insertFn) => {
        const form = new FormData();
        form.append("file", file);
        uploadImage(form)
          .then(res => {
            if (res.errno == 0) {
              message("上传成功", { type: "success" });
              insertFn(res.data.url, res.data.alt, res.data.href);
            } else {
              message("上传失败:" + res.message, { type: "error" });
            }
          })
          .catch(error => {
            message("上传失败:" + JSON.stringify(error), { type: "error" });
          });
      }
    },
    uploadVideo: {
      customUpload: async (file, insertFn) => {
        const form = new FormData();
        form.append("file", file);
        uploadVideo(form)
          .then(res => {
            if (res.errno == 0) {
              message("上传成功", { type: "success" });
              insertFn(res.data.url, res.data.poster);
            } else {
              message("上传失败:" + res.message, { type: "error" });
            }
          })
          .catch(error => {
            message("上传失败:" + JSON.stringify(error), { type: "error" });
          });
      }
    }
  }
};

const handleCreated = editor => {
  editorRef.value = editor; // 记录 editor 实例，重要！
};

// 组件销毁时，也及时销毁编辑器
onBeforeUnmount(() => {
  const editorTitle = editorRef.value;
  if (editorTitle != null) {
    editorTitle.destroy();
  }
});
</script>

<template>
  <el-form
    ref="ruleFormRef"
    :model="newFormInline"
    :rules="formRules"
    label-width="82px"
  >
    <Motion>
      <el-form-item label="选项：" prop="name">
        <el-select
          v-model="newFormInline.name"
          filterable
          placeholder="请选择"
          clearable
          class="!w-[220px]"
        >
          <el-option
            :label="item.id"
            :value="item.id"
            v-for="item in questionData0"
            :key="item.id"
          />
        </el-select>
      </el-form-item>
    </Motion>

    <Motion>
      <el-form-item label="内容：" prop="content">
        <Toolbar
          style="border-bottom: 1px solid #ccc"
          :editor="editorRef"
          :defaultConfig="toolbarConfig"
          :mode="mode"
        />
        <Editor
          style="width: 100%; height: 300px; overflow-y: hidden"
          v-model="newFormInline.content"
          :defaultConfig="editorConfig"
          :mode="mode"
          @onCreated="handleCreated"
        />
      </el-form-item>
    </Motion>
  </el-form>
</template>
